import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import datetime
import jieba
from copy import deepcopy

# plt.style.use('fivethirtyeight') # 使用背景颜色
plt.style.use('seaborn') # 使用背景颜色

plt.rcParams['font.sans-serif'] = ['Arial Unicode MS'] # 设置字体，解决绘图不支持中文问题

# 读取数据
df = pd.read_csv('./data/beautymakeup in T-mall.csv')
# print(df.head())
# print(df.info())

# 哪些字段有空值
# print(df.isnull().any())

# 为空的数据
# print(df[df.isnull().T.any()])

# 查看为空数据量
# df[df.isnull().T.any()].info()

# 备份df --> df2
# df2 = deepcopy(df)
# df2.dropna(inplace=True)
# df2.info()
# print(df2[df2.T.any()])

# df = df2
# 重复数据
print(df.duplicated(keep=False).value_counts())
print(df[df.duplicated(keep=False)].head())

df = df.drop_duplicates(keep='first')
df.info()

df['update_time'] = df['update_time'].apply(
    lambda a: datetime.datetime.strptime(a, '%Y/%m/%d')
)
df.head()

# 读取分类表
df_category = pd.read_csv('./data/category_new.csv')
# print(df_category)

# 建立从关键字到类别的映射关系
keyword_to_category_map = {}
for index in df_category.index:
    df_loc = df_category.loc[index]
    main_type = df_loc['main_type']
    sub_type = df_loc['sub_type']
    keyword_str = df_loc['keywords']
    keywords = keyword_str.split('|')
    for keyword in keywords:
        keyword_to_category_map[keyword] = dict(main_type=main_type , sub_type=sub_type)
print(keyword_to_category_map)

# 添加分类
def get_type(title,keyword_to_category_map=keyword_to_category_map,default='其他'):
    '''
    :return {'main_type':'','sub_type':''}
    '''
    for keyword in keyword_to_category_map.keys():
        if keyword in title:
            return keyword_to_category_map[keyword]
    return {'main_type':default,'sub_type':default}

for index in df.index:
    title = df.loc[index, 'title']
    type_dict = get_type(title)
    main_type = type_dict['main_type']
    sub_type = type_dict['sub_type']
    df.loc[index,'main_type'] = main_type
    df.loc[index,'sub_type'] = sub_type
print(df.head())

# 销售额
df['total_sale'] = df['sale_count']  * df['price']
df.head()

# 修改列名，重置索引
df.rename( columns={ 'update_time':'update_date'},inplace=True)
df.head(1)

# 重置索引
df.reset_index(inplace=True,drop=True)
df.head(1)

# 一级分类的营业额
df2_1 = df.groupby('main_type')[['total_sale','sale_count']].sum()
print(df2_1.head())
current_palette = sns.color_palette()

fig , axs = plt.subplots(1,2)
ax0 = df2_1['total_sale'].plot.pie(
    ax=axs[0],
    explode=[0.05] * df2_1['total_sale'].shape[0],
    autopct='%3.2f%%', #数值保留固定小数位
)

ax0.set_ylabel('')
ax0.set_title('1级分类总销售额占比')
# ax0.legend(loc="best",bbox_to_anchor=(1, 1))   # 与plt.legend(loc=1)等价

ax1 = df2_1['sale_count'].plot.pie(
    ax=axs[1],
    explode = [0.05] * df2_1['sale_count'].shape[0],
    autopct = '%3.2f%%', #数值保留固定小数位,
    colors=current_palette
)
ax1.set_ylabel('')
ax1.set_title('1级分类总销量占比')
print(ax1)

# 对比2级分类的销售额和销量
df2_2 = df.groupby(['main_type','sub_type'])[['total_sale','sale_count']].sum().reset_index()
df2_2.head()







